package test.image;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class VerifyImage {
    private int width = 70;
    private int height = 35;
    private Random random = new Random();
    private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
    private String[] fonts = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };
    private Color backgroundColor = new Color(255, 255, 255);
    private String text;

    /**
     * @description 向out输出一张JPG图片
     * @param image
     * @param out
     * @throws IOException
     */
    public static void output(BufferedImage image, OutputStream out) throws IOException {
        ImageIO.write(image, "JPEG", out);
    }

    /**
     * @description 获取一张随机的验证码图片
     * @return
     */
    public BufferedImage getVerifyImage() {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D pen = (Graphics2D) image.getGraphics();
        StringBuilder str = new StringBuilder();
        pen.setColor(backgroundColor);
        pen.fillRect(0, 0, width, height);
        for (int i = 0; i < 4; i++) {
            String randomStr = String.valueOf(this.getRandomChar());
            str.append(randomStr);
            float x = i * 1.0F * width / 4;
            pen.setColor(this.getRandomColor());
            pen.setFont(this.getRandomFont());
            pen.drawString(randomStr, x, height - 5);
        }
        this.text = str.toString();
        this.drawRandomLine(image, 3);
        return image;
    }

    /**
     * @description 获取验证码图片对应的上面的文字, 必须在getVerifyImage后调用
     * @return
     */
    public String getText() {
        return text;
    }

    private Color getRandomColor() {
        int red = random.nextInt(150);
        int green = random.nextInt(150);
        int blue = random.nextInt(150);
        return new Color(red, green, blue);
    }

    private char getRandomChar() {
        int index = random.nextInt(codes.length());
        return codes.charAt(index);
    }

    private Font getRandomFont() {
        int index = random.nextInt(fonts.length);
        int style = random.nextInt(4);// 生成随机的样式, 0(无样式), 1(粗体), 2(斜体), 3(粗体+斜体)
        int size = random.nextInt(5) + 24; // 生成随机字号, 24 ~ 28
        return new Font(fonts[index], style, size);
    }

    private void drawRandomLine(BufferedImage image, int drawLineNum) {
        int lineNum = drawLineNum; // 画3条线
        Graphics2D pen = (Graphics2D) image.getGraphics();
        for (int i = 0; i < lineNum; i++) {
            int xPointStart = random.nextInt(width);
            int yPointStart = random.nextInt(height);
            int xPointEnd = random.nextInt(width);
            int yPointEnd = random.nextInt(height);
            pen.setStroke(new BasicStroke(1.5F)); // 设置线条的宽度
            pen.setColor(this.getRandomColor());
            pen.drawLine(xPointStart, yPointStart, xPointEnd, yPointEnd);
        }
    }

}
